package com.atguigu.edusugar.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edusugar.bean.Cm;
import com.atguigu.edusugar.bean.Course;
import com.atguigu.edusugar.bean.KW;
import com.atguigu.edusugar.bean.Province;
import com.atguigu.edusugar.service.TradeService;
import com.atguigu.edusugar.service.TrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lzc
 * @Date 2022/10/21 14:43
 */

@RestController
public class SugarController {
    // https://natapp.cn/#

    
    @Autowired
    TrafficService trafficService;

    // ${USER_HOST}/sugar/traffic?date=20221027
    @RequestMapping("/sugar/traffic")
    public String traffic(Integer date) {
        // 如果是 null, 表示要查的是当天
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<Map<String, Object>> list = trafficService.traffic(date);
        
        
        // 将来需要根据 hour 来获取没每行数据
        // key(string)-> value
        HashMap<String, Map<String, Object>> hourToMap = new HashMap<>();
        for (Map<String, Object> map : list) {
            String hour = map.get("hour").toString();
            hourToMap.put(hour, map);
        }
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (int hour = 0; hour < 24; hour++) {
            categories.add(hour);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        HashMap<String, Object> defaultValue = new HashMap<>();
        defaultValue.put("pv", 0L);
        defaultValue.put("sv", 0L);
        defaultValue.put("uv", 0L);
        
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        pv.put("data", pvData);
        series.add(pv);
        
        
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        uv.put("data", uvData);
        series.add(uv);
        
        JSONObject sv = new JSONObject();
        sv.put("name", "sv");
        JSONArray svData = new JSONArray();
        sv.put("data", svData);
        series.add(sv);
        
        
        for (int hour = 0; hour < 24; hour++) {
            defaultValue.put("hour", hour);
            // 从 map 中根据 hour 取出对应的 map 集合, 如果 hour 不存在, 则返回默认值
            Map<String, Object> map = hourToMap.getOrDefault(hour + "", defaultValue);
            uvData.add(map.get("uv"));
            pvData.add(map.get("pv"));
            svData.add(map.get("sv"));
        }
        
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
    }

    //${USER_HOST}/sugar/kw?date=20221026
    @RequestMapping("/sugar/kw")
    public String kw(Integer date) {
        // 如果是 null, 表示要查的是当天
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
    
        List<KW> list = trafficService.kw(date);
    
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
    
        for (KW kw : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getScore());
            data.add(obj);
        }
    
        result.put("data", data);
        
        return result.toJSONString();
    }

    @Autowired
    TradeService tradeService;

    //${USER_HOST}/sugar/gmv
    @RequestMapping("/sugar/gmv")
    public String gmv(Integer date) {
        // 如果是 null, 表示要查的是当天
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        BigDecimal gmv = tradeService.gmv(date);

        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        result.put("data", gmv);

        return result.toJSONString();
    }

    //${USER_HOST}/sugar/gmv/course
    @RequestMapping("/sugar/gmv/course")
    public String gmvBySpu(Integer date) {
        // 如果是 null, 表示要查的是当天
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        List<Course> list = tradeService.gmvByCourse(date);
        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();

        JSONArray categories = new JSONArray();
        for (Course course : list) {
            categories.add(course.getCourse_name());
        }
        data.put("categories", categories);

        JSONArray series = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("name", "course_name");
        JSONArray data1 = new JSONArray();
        for (Course course : list) {
            data1.add(course.getOrder_amount());
        }
        obj.put("data", data1);
        series.add(obj);
        data.put("series", series);

        result.put("data", data);

        return result.toJSONString();
    }

    //${USER_HOST}/sugar/gmv/cm   饼图
    @RequestMapping("/sugar/gmv/cm")
    public String gmvByCm(Integer date) {
        // 如果是 null, 表示要查的是当天
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        List<Cm> list = tradeService.gmvByCm(date);
        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONArray data = new JSONArray();

        for (Cm cm : list) {
            JSONObject obj = new JSONObject();

            obj.put("name", cm.getCategory_name());
            obj.put("value", cm.getOrder_amount());

            data.add(obj);
        }


        result.put("data", data);

        return result.toJSONString();
    }

    //${USER_HOST}/sugar/province  省份地图
    @RequestMapping("/sugar/province")
    public String province(Integer date) {
        // 如果是 null, 表示要查的是当天
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        List<Province> list = tradeService.province(date);

        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();

        JSONArray mapData = new JSONArray();

        for (Province province : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", province.getProvince_name());
            obj.put("value", province.getOrder_amount());
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_count());
            obj.put("tooltipValues", tooltipValues);

            mapData.add(obj);
        }

        data.put("mapData", mapData);

        data.put("valueName", "销售额");

        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);

        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);

        result.put("data", data);

        return result.toJSONString();
    }
}
